import { configureStore } from '@reduxjs/toolkit'
import {
  shallowEqual,
  TypedUseSelectorHook,
  useDispatch,
  useSelector
} from 'react-redux'
import appSlice from './module/app'
import loginSlice from './module/login'
import forgetPwdSlice from './module/forget-pwd'
import websocketSlice from './module/websocket'
import pageCommonSlice from './module/main/page-common'
import roleListSlice from './module/main/role-list'
import menuListSlice from './module/main/menu-list'

const store = configureStore({
  reducer: {
    app: appSlice,
    login: loginSlice,
    forgetPwd: forgetPwdSlice,
    pageCommon: pageCommonSlice,
    websocket: websocketSlice,
    roleList: roleListSlice,
    menuList: menuListSlice
  }
})

/**
 * ts里对useSelector进行改造，让新的useMySelector可以自动获取state的数据类型
 */
// 1.获取store.getState的函数类型
type GetStateFnType = typeof store.getState
// 2.获取函数返回值类型(也就是我们要的数据对象类型)
export type IRootState = ReturnType<GetStateFnType>
// 3.传入泛型成功改造useSelector
export const useMySelector: TypedUseSelectorHook<IRootState> = useSelector

/**
 * 为了使用更方便，也顺便将useDispatch改造(这里非必要的)
 */
type DispatchType = typeof store.dispatch
export const useMyDispatch: () => DispatchType = useDispatch
export const myShallowEqual = shallowEqual

export default store
